Security News
ESLint is Now Language-Agnostic: Linting JSON, Markdown, and Beyond
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
koa-convert
Advanced tools
The koa-convert npm package is designed to help transition from Koa's old middleware signature (generator functions) to the new middleware signature (async functions). It wraps Koa's old generator-based middleware to be compatible with the new Koa v2.x async middleware signature. This allows developers to use legacy middleware that hasn't yet been updated to the new format, facilitating a smoother upgrade path to Koa v2.x.
Converting generator-based middleware
This feature allows you to wrap old Koa middleware that uses generator functions into a form that's compatible with Koa v2.x's async middleware. The code sample demonstrates how to convert a simple timing middleware to the new format.
const convert = require('koa-convert');
const oldMiddleware = function *(next) {
const start = new Date;
yield next;
const ms = new Date - start;
console.log(`${this.method} ${this.url} - ${ms}ms`);
};
app.use(convert(oldMiddleware));
koa-compose is a package for composing Koa middleware. While it doesn't directly convert middleware from the old to the new format, it's useful for managing middleware in Koa applications, especially when dealing with async functions. It complements koa-convert by providing a structured way to combine multiple middleware functions into a single middleware.
koa-adapter is another package that aims to provide compatibility layers for Koa middleware, though it's less focused on converting from the old generator-based middleware to the new async-based middleware. Instead, it focuses on adapting Express middleware to be used in Koa applications, serving a slightly different but related purpose to koa-convert.
Convert koa legacy ( 0.x & 1.x ) generator middleware to modern promise middleware ( 2.x ).
It could also convert modern promise middleware back to legacy generator middleware ( useful to help modern middleware support koa 0.x or 1.x ).
It should be able to convert any legacy generator middleware to modern promise middleware ( or convert it back ).
Please let me know ( send a issue ) if you fail to do so.
$ npm install koa-convert
const Koa = require('koa') // koa v2.x
const convert = require('koa-convert')
const app = new Koa()
app.use(modernMiddleware)
app.use(convert(legacyMiddleware))
app.use(convert.compose(legacyMiddleware, modernMiddleware))
function * legacyMiddleware (next) {
// before
yield next
// after
}
function modernMiddleware (ctx, next) {
// before
return next().then(() => {
// after
})
}
In koa 0.x and 1.x ( without experimental flag ), app.use
has an assertion that all ( legacy ) middleware must be generator function and it's tested with fn.constructor.name == 'GeneratorFunction'
at here.
Therefore, we can distinguish legacy and modern middleware with fn.constructor.name == 'GeneratorFunction'
.
app.use(legacyMiddleware)
is everywhere in 0.x and 1.x and it would be painful to manually change all of them to app.use(convert(legacyMiddleware))
.
You can use following snippet to make migration easier.
const _use = app.use
app.use = x => _use.call(app, convert(x))
The above snippet will override app.use
method and implicitly convert all legacy generator middleware to modern promise middleware.
Therefore, you can have both app.use(modernMiddleware)
and app.use(legacyMiddleware)
and your 0.x or 1.x should work without modification.
Complete example:
const Koa = require('koa') // v2.x
const convert = require('koa-convert')
const app = new Koa()
// ---------- override app.use method ----------
const _use = app.use
app.use = x => _use.call(app, convert(x))
// ---------- end ----------
app.use(modernMiddleware)
// this will be converted to modern promise middleware implicitly
app.use(legacyMiddleware)
function * legacyMiddleware (next) {
// before
yield next
// after
}
function modernMiddleware (ctx, next) {
// before
return next().then(() => {
// after
})
}
convert()
Convert legacy generator middleware to modern promise middleware.
modernMiddleware = convert(legacyMiddleware)
convert.compose()
Convert and compose multiple middleware (could mix legacy and modern ones) and return modern promise middleware.
composedModernMiddleware = convert.compose(legacyMiddleware, modernMiddleware)
// or
composedModernMiddleware = convert.compose([legacyMiddleware, modernMiddleware])
convert.back()
Convert modern promise middleware back to legacy generator middleware.
This is useful to help modern promise middleware support koa 0.x or 1.x.
legacyMiddleware = convert.back(modernMiddleware)
MIT
FAQs
convert modern Koa legacy generator-based middleware to promise-based middleware
The npm package koa-convert receives a total of 1,859,977 weekly downloads. As such, koa-convert popularity was classified as popular.
We found that koa-convert demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint has added JSON and Markdown linting support with new officially-supported plugins, expanding its versatility beyond JavaScript.
Security News
Members Hub is conducting large-scale campaigns to artificially boost Discord server metrics, undermining community trust and platform integrity.
Security News
NIST has failed to meet its self-imposed deadline of clearing the NVD's backlog by the end of the fiscal year. Meanwhile, CVE's awaiting analysis have increased by 33% since June.